<!-- 友链编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'易仓订单导入':'易仓订单导入'"
    :visible="visible"
    width="950px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="140px">

      <el-row :gutter="15">
        <el-col :sm="12">
          <el-form-item label="平台:" prop="platform_code">
            <el-select  style="width: 180px" clearable filterable class="form_width" v-model="form.platform_code">
              <el-option v-for="(item,index) in searchInfo.platform_list" :key="index" :label="item"
                         :value="index"/>
            </el-select>&nbsp;&nbsp;
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="平台订单号:" prop="order_code">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.order_code"
          placeholder="请输入单号,多个以;分开"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="importOrder"
        :loading="loading">导入
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'EccangImport',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    searchInfo: Object,
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({platform_code: 'amazon'}, this.data),
      // 表单验证规则
      rules: {
        platform_code: [
          {required: true, message: '请选择平台', trigger: 'blur'}
        ],
        order_code: [
          {required: true, message: '请至少填写一个订单号', trigger: 'blur'}
        ],

      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({platform_code:'amazon'}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {platform_code:'amazon'};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 导入订单 */
    importOrder() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/OrderAndCustomersOrder/importOrder', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message({
                dangerouslyUseHTMLString: true,
                showClose: true,
                message:res.data.msg,
                type: 'error',
                duration: 0
              })
              // this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
